#include "Material.h"
#include "Runtime/Rendering/MaterialRenderProxy.h"
#include "MaterialShared.h"
#include "Runtime/Allocator/Memory.h"
#include "Runtime/Rendering/MeshPassProcessor.h"

namespace Alice
{
    Material::Material()
    {
        Memory::Zero(mShaderPipelines,sizeof(ShaderPipeline*)*EMeshPass::Num);
    }
    void Material::SetShader(EMeshPass::Type inMeshPass,EShaderFrequency inShaderFrequency, Shader* inShader)
    {
        if(mShaderPipelines[inMeshPass]==nullptr)
        {
            mShaderPipelines[inMeshPass]=new ShaderPipeline;
        }
        mShaderPipelines[inMeshPass]->SetShader(inShaderFrequency,inShader);
    }
    
    void Material::CacheResourceShadersForRendering()
    {
        mDefaultMaterialInstance=new DefaultMaterialInstance(this);
        std::vector<MaterialSharedResource*> resourcesToCache;
        MaterialSharedResource*currentResource=new MaterialSharedResource(this);
        mMaterialSharedResources.push_back(currentResource);
        resourcesToCache.push_back(currentResource);
        CacheShadersForResources(resourcesToCache);
    }
    void Material::CacheShadersForResources(std::vector<MaterialSharedResource*>& inResourcesToCache)
    {
        for (uint32 i=0;i<inResourcesToCache.size();i++)
        {
            inResourcesToCache[i]->CacheShaders();
        }
    }

}
